package com.zhou.listener.library;

import com.zhou.listener.library.baselistener.IBaseFileDownloadListener;
import com.zhou.listener.library.utils.IKey;

import java.util.HashMap;

public class ListenerManager {
    //构建单例
    private static ListenerManager instance=new ListenerManager();
    private ListenerManager(){}
    public static ListenerManager getInstance() {
        return instance;
    }


    //构建存储listener 类的map结构
    private HashMap<String,IBaseFileDownloadListener> downloadMap = new HashMap<>();

    public <T extends IBaseFileDownloadListener>T addListener(IKey key, T o){
        downloadMap.put(key.getKey(), o);
        return o;
    }

    public <T extends IBaseFileDownloadListener>T getListener(IKey key){
        if(key==null){
            return null;
        }
        return (T)downloadMap.get(key.getKey());
    }

    public boolean containsKey(IKey key){
        if(key==null){
            return false;
        }else{
            return downloadMap.containsKey(key.getKey());
        }
    }

    public void removeAll(){
        downloadMap.clear();
    }
}
